/*
	Magnetic Tank
	Emmanuel Julien, François Gutherz 2010
*/

		Include("data/scripts/locale.nut")
		Include("data/scripts/audio.nut")
		Include("data/scripts/ui.nut")
		Include("data/scripts/inventory.nut")

g_sound_enable			<-	false

/*
*/
class	ScreenBase
{
	spawn_name		=	0
	status			=	"ok"

	player_item		=	0

	project_script	=	0

	/*
		Test if the level should end on a given trigger condition.
	*/
	function	testExitTrigger(scene, trigger_name)
	{
		local	exit_trigger = ItemCastToTrigger(SceneFindItem(scene, trigger_name))
		return TriggerTestItem(exit_trigger, player_item)
	}

	function	spawnPlayer(scene)
	{
		print("ScreenBase::spawnPlayer() : spawn_name = " + spawn_name)
		local	spawn = SceneFindItem(scene, spawn_name)
		local	group = SceneFromFileStoreGroup(scene, "data/instances/vehicles/steam_tank/scene.nms", FlagLoadAll)
		player_item = GroupFindItem(group, "tank")
		ItemSetMatrix(player_item, ItemGetMatrix(spawn))
	}

	function	OnSetup(scene)
	{
		project_script = ProjectGetScriptInstance(g_project)
		print("ScreenBase::OnSetup()")
		spawn_name = project_script.spawn_name
		spawnPlayer(scene)
	}
}

function	SceneRenderSetup(_scene)
{
	local	_list = SceneGetItemList(_scene)
	foreach(_item in _list)
	{
		ItemRenderSetup(_item, g_factory)
	}
}

/*
*/
class	Project
{
	screen_name			=	"level_1_0"
	spawn_name			=	"spawn_0"

	states				=	0

	project_scene		=	0
	screen_scene		=	0
	screen_script		=	0

	hud_scene			=	0
	hud_ui				=	0

	tank				=	{	
								current_velocity = 0,
								current_rotation = 0,
								energy = -1,
								shield = -1
							}

	inventory			=	0

	audio				=	{
								player 	= 0,
								enemy	= 0,
								environment = 0,
								music = 0
							}

	//	Game state methods :
	//	Keeps track of what's destroyed/unlocked/taken in the game
	//	A unique key (ID) is computed for each item, based on the current level,
	//	the item's name, its X/Z position (no Y), and the attribute name
	function	SetItemStateEnabled(item, flag)
	{		states[CreateItemHashKey(screen_name, item, "enabled")] = flag	}

	function	GetItemStateEnabled(item)
	{
		try
		{		return (states[CreateItemHashKey(screen_name, item, "enabled")])	}
		catch(e)
		{		states[CreateItemHashKey(screen_name, item, "enabled")] <- true		}
		return true
	}

	function	SetItemStateClosed(item, flag)
	{		states[CreateItemHashKey(screen_name, item, "closed")] = flag	}

	function	GetItemStateClosed(item)
	{
		try
		{		return (states[CreateItemHashKey(screen_name, item, "closed")])	}
		catch(e)
		{		states[CreateItemHashKey(screen_name, item, "closed")] <- true		}
		return true
	}

	//	Level load/unload methods :
	function	LoadScreenScene(project)
	{
		local	_scene_path
		_scene_path = "data/levels/" + screen_name + ".nms"

		if (FileExists(_scene_path))
		{
			project_scene = ProjectInstantiateScene(project, _scene_path)
			ProjectAddLayer(project, project_scene, 0.5)
			return ProjectSceneGetInstance(project_scene)
		}
		else
		{
			print("Project::loadLevel() : Level ''" + _scene_path + "'' cannot be found !")
			return 0
		}
	}

	function	unloadLevel(project)
	{		ProjectUnloadScene(project, project_scene)	}

	function	loadLevel(project)
	{
		print("--------------------")
		print("Project::loadLevel()")
		print("--------------------")

		// Load the level.
		screen_scene = LoadScreenScene(project)
		hud_ui = HudUI()

		//	Display Room Name
		local	room_name 
		try
		{
			room_name = g_locale.room_names[screen_name]
		}
		catch(e)
		{
			room_name = screen_name
		}
		
		print("Project::loadLevel() : screen_name = " + screen_name + ", room_name = " + room_name)
		hud_ui.UpdateRoomName(room_name)
		hud_ui.UpdateInventory(inventory)

		// Setup scene.
		SceneRenderSetup(screen_scene)

		// Grab the level main scene.
		screen_script = SceneGetScriptInstanceFromClass(screen_scene, "Level")

		UISetGlobalFadeEffect(SceneGetUI(screen_scene), 1)
		UISetGlobalFadeColor(SceneGetUI(screen_scene), 0, 0, 0)
		UISetCommandList(SceneGetUI(screen_scene), "globalfade 0, 1; globalfade 0.5, 0;")
	}

	function	SetupAudio()
	{
		audio.player = TankAudio()
		audio.enemy = MobileAudio("saucer")
		audio.environment = EnvironmentAudio()
		audio.music = Music()
	}

	function	OnSetup(project)
	{
		states = {}
		SetupAudio()

		// Setup UI
		//hud_scene = ProjectInstantiateScene(project, "data/ui/game_hud.nms")
		//ProjectAddLayer(project, hud_scene, 0.0)
		//HudUI(ProjectSceneGetInstance(hud_scene))

		//	Play music
		audio.music.PlayMusicTrack(0)

		// Inventory
		inventory = Inventory()

		// Load initial level.
		loadLevel(project)
	}

	function	OnUpdate(project)
	{
		// Check the game flow table for the next action to take.
		if	(screen_script.status)
		{

			switch (screen_script.status)
			{
				case "next_level":
				{
					unloadLevel(project)
					loadLevel(project)
					screen_script.status = "ok"
					break;
				}
			}
		}
	}
}
